package dome3;

public class Text {
    public static void main(String[] args) {
        Animal animal1 = new Dog("小黑",2);//向上转型
        Animal animal2 = new Cat("小花",1);//向上转型
        animal1.eat();
        animal2.eat();
        //向下转型
        if(animal1 instanceof Dog) {//判断animal1是否为Dog的实例对象
            Dog dog =(Dog)animal1;//向下转型
            dog.bark();
        }
        if(animal2 instanceof Cat) {//判断animal2是否为Cat的实例对象
            Cat cat =(Cat)animal2;//向下转型
            cat.acting();
        }
    }
    public static void eat(Animal a) {
        a.eat();
    }

    public static Animal buyAnimal(String var) {
        if("狗".equals(var)) {
            return new Dog("狗狗",2);
        }else if("猫".equals(var)) {
            return new Cat("猫猫",1);
        }else {
            return null;
        }
    }

    public static void main1(String[] args) {
        Animal dog = new Dog("小黑",2);//向上转型
        Animal cat = new Cat("小花",1);//向上转型
        dog.eat();
        cat.eat();
        Dog dog2 = new Dog("小黑",2);
        Cat cat2 = new Cat("小花",1);
        eat(dog2);
        eat(cat2);
        Animal animal1 = buyAnimal("狗");
        Animal animal2 = buyAnimal("猫");
        animal1.eat();
        animal2.eat();
    }
}
